#!/bin/sh

###############################################################
# installacme -- Acme App installation script.                #
# Julian Smart, January 2005                                  #
# Usage: installacme [optional target directory]              #
###############################################################

########### Edit these variables for your system. #############

# Editor
MYEDITOR=$EDITOR
if [ "$MYEDITOR" = "" ]
then
  MYEDITOR=emacs
fi

# Pager
MYPAGER=$PAGER
if [ "$MYPAGER" = "" ]
then
  MYPAGER=more
fi

# Acme directory to install to.
ACMEDIR=~/Acme-__ACMEVERSION__
if [ ! "$1" = "" ]
then
  ACMEDIR="$1"
fi

# Directory to put the launch script
APPBINDIR="$ACMEDIR"

# Archive prefix
ARCHIVE=AcmeData

# Current directory
CURRENTDIR=`pwd`

install_from_gzip()
{
  if [ ! -d "$ACMEDIR" ]
  then
    mkdir -p "$ACMEDIR"
  fi

  if [ ! -d "$ACMEDIR" ]
  then
    echo "*** Sorry, the directory $ACMEDIR could not be created."
  else
    cd "$ACMEDIR"
    if [ -f $CURRENTDIR/$ARCHIVE".tar.gz" ]
    then
      echo Unarchiving $CURRENTDIR/$ARCHIVE".tar.gz"
      tar xfz $CURRENTDIR/$ARCHIVE".tar.gz"
    else
      echo $CURRENTDIR/$ARCHIVE".tar.gz" not found. Please place in the current directory.
    fi
    cd $CURRENTDIR
  fi
}

# Select a target
select_dir()
{
  dirok="n"
  while [ "$dirok" != "y" ]
  do
    echo "Select the directory to install Acme into"
    echo "--------------------------------------------------------------"
    echo ""
    echo "This is where the binaries and data will be copied to."
    echo "They can go anywhere you want, but make sure you have the"
    echo "appropriate permissions if copying outside your home area."
    echo ""
    echo "Current value: " $ACMEDIR
    echo ""
    echo "New directory: (return for existing value):"
    read inp
    if [ "$inp" != "" ]
    then
      ACMEDIR=`echo $inp | sed "s|~|\${HOME}|"`
    fi

    dirok="y"

    if [ -d "$ACMEDIR" ]
    then
      echo "This directory already exists. Do you wish to choose another? (y/n)"
      read inp
      if [ "$inp" = "y" ]
      then
        dirok="n"
      fi
    fi
    APPBINDIR="$ACMEDIR"

    # If we're not going around the loop again, try creating this directory.
    if [ "$dirok" = "y" ]
    then
        canremove="n"
        if [ ! -d "$ACMEDIR" ]
        then
          canremove="y"
        fi

        mkdir -p "$ACMEDIR"
        if [ ! -d "$ACMEDIR" ]
        then
          dirok="n"
          echo ""
          echo "*** Sorry, this directory could not be created. You may need to"
          echo "quit this script, log on as root, and run the script again."
          echo "Alternatively, try choosing a directory in your home area."
          echo ""
          echo "Quit the script now? (y/n)"
          read inp
          if [ "$inp" = "y" ]
          then
            echo "Goodbye, come back soon!"
            exit
          fi
        else
          # Now we kow we can create it, clean it up
          if [ "$canremove" = "y" ]
          then
            rmdir "$ACMEDIR"
          fi
        fi
    fi
  done
}

# Select a dir for the scripts to be placed
select_script_dir()
{
  dirok="n"
  while [ "$dirok" != "y" ]
  do
    echo "Select the directory to install the launch script into."
    echo "--------------------------------------------------------------"
    echo ""
    echo "We will create a script called acme which"
    echo "invoke this application. You may wish to place these in your"
    echo "local bin directory, or they can be placed in the Acme"
    echo "install directory (the default location)."
    echo ""
    echo "Current value: " $APPBINDIR
    echo ""
    echo "New directory: (return for existing value):"
    read inp
    if [ "$inp" != "" ]
    then
      APPBINDIR=`echo $inp | sed "s|~|\${HOME}|"`
    fi

    dirok="y"

    # If we're not going around the loop again, try creating this directory.
    if [ "$dirok" = "y" ]
    then
        canremove="n"
        if [ ! -d "$APPBINDIR" ]
        then
          canremove="y"
        fi

        mkdir -p "$APPBINDIR"
        if [ ! -d "$APPBINDIR" ]
        then
          dirok="n"
          echo ""
          echo "*** Sorry, this directory could not be created. You may need to"
          echo "quit this script, log on as root, and run the script again."
          echo "Alternatively, try choosing a directory in your home area."
          echo ""
          echo "Quit the script now? (y/n)"
          read inp
          if [ "$inp" = "y" ]
          then
            echo "Goodbye, come back soon!"
            exit
          fi
        else
          # Now we kow we can create it, clean it up
          if [ "$canremove" = "y" ]
          then
            rmdir "$APPBINDIR"
          fi
        fi
    fi
  done
}

# Write scripts
write_launch_script()
{
  if [ ! -d "$APPBINDIR" ]
  then
    mkdir -p "$APPBINDIR"
  fi

  if [ ! -d "$APPBINDIR" ]
  then
    echo "*** Sorry, the directory $APPBINDIR could not be created."
  else
    echo "#!/bin/sh" > "$APPBINDIR"/acme
    echo "# A script to run Acme." >> "$APPBINDIR"/acme
    echo "# Set the LANG variable to not use UTF-8 since it does not work well with Acme." >> "$APPBINDIR"/acme
    echo "export LANG=\`echo \$LANG | sed \"s/.UTF-8//g\"\`" >> "$APPBINDIR"/acme
    echo "export ACMEDIR=\"$ACMEDIR\"" >> "$APPBINDIR"/acme
    echo "\"$ACMEDIR\"/acme" >> "$APPBINDIR"/acme

    chmod a+x "$APPBINDIR"/acme
    if [ -f "$APPBINDIR"/acme ]
    then
      echo Wrote "$APPBINDIR"/acme
    fi
  fi
}

### View readme ###

view_readme()
{
  if [ ! -f "$ACMEDIR"/readme.txt ]
  then
    echo "Sorry -- you must unarchive the distribution before you can"
    echo "view the readme."
  else
    $MYPAGER "$ACMEDIR"/readme.txt
  fi
}

### Launch the application ###

launch_application()
{
  if [ ! -f "$APPBINDIR"/acme ]
  then
    echo "Sorry -- you must unarchive the distribution before you can"
    echo "launch the application."
  else
    "$APPBINDIR"/acme
  fi
}

preliminary_notes()
{
  selectdone1=0
  ans="n"
  while [ "$ans" != "y" ]
  do
    echo
    echo The directory to install the applications to is $ACMEDIR.
    echo The directory to install the launch script to is "$APPBINDIR".
    echo
    echo "Please check these settings carefully."
    echo "Are they correct? (y/n)"
    echo

    read ans

    if [ "$ans" != "y" ]
    then
      select_dir
      select_script_dir
    fi
  done
}

  ############# MAIN BIT OF THE SCRIPT #################
  clear
  echo
  echo "--------------------------------------------------------------"
  echo "Welcome to the Acme __ACMEVERSION__ installation."
  echo "--------------------------------------------------------------"
  echo
  echo "This script will install Acme from the archive:"
  echo ""
  echo "  "$ARCHIVE".tar.gz"
  echo ""

  select_dir
  select_script_dir
  preliminary_notes

  useroption='none'
  while [ "$useroption" != "0" ]
  do
    builtit=""
    installfirst=""

    if [ -f "$APPBINDIR/acme" ] && [ -f "$ACMEDIR/readme.txt" ]
    then
      builtit=" (done)"
    else
      installfirst=" (install first)"
    fi

    echo ""
    echo "--------------------------------------------------------------"
    echo "Acme __ACMEVERSION__ Installation Menu"
    echo "--------------------------------------------------------------"
    echo ""

    if [ "$builtit" = "" ]
    then
      echo "Ready to install application to $ACMEDIR"
      echo "and launch script to $APPBINDIR."
      echo ""
    fi
    echo "Please type a number followed by return to choose an option:"
    echo ""
    echo " 1) Install Acme and its launch script"$builtit"."
    echo " 2) View the readme, with a few words of introduction"$installfirst"."
    echo " 3) Launch Acme"$installfirst"."
    echo " 4) Change the application installation directory."
    echo " 5) Change the launch script installation directory."
    echo "--------------------------------------------------------------"
    echo " 0) Exit from installation."
    echo
    read useroption

    if [ "$useroption" = "1" ]
    then
      install_from_gzip
      write_launch_script

      echo ""

      if [ -f "$APPBINDIR/acme" ] && [ -f "$ACMEDIR/readme.txt" ]
      then
        echo "*** Success! Acme was installed."
      else
        echo "*** Sorry, Acme was not installed correctly."
        echo "Please check you have permission for the paths you specified."
      fi
    fi
    if [ "$useroption" = "2" ]
    then
      view_readme
    fi
    if [ "$useroption" = "3" ]
    then
      launch_application
    fi
    if [ "$useroption" = "4" ]
    then
      select_dir
    fi
    if [ "$useroption" = "5" ]
    then
      select_script_dir
    fi
  done
  clear
  echo ""
  if [ -f "$APPBINDIR/acme" ] && [ -f "$ACMEDIR/readme.txt" ]
  then
    echo "Goodbye! Your Acme installation was successful."
    echo "If you have "$APPBINDIR" in your PATH you can simply type"
    echo "'acme' to get started; otherwise run "$ACMEDIR/acme"."
    echo ""
    echo "You may now delete "Acme-__ACMEVERSION__.tar.gz" and AcmeData.tar.gz."
    echo "To uninstall Acme in the future, remove the following files:"
    echo ""
    echo "$APPBINDIR"/acme
    echo $ACMEDIR
  else
    echo "Goodbye!"
  fi
  echo ""
  echo "For support, please visit:"
  echo ""
  echo "http://www.acmecorp.com"
  echo ""


